package com.shanghaionstar.smart.util;

import android.content.Context;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.onstar.cn.framework.network.http.Sender;
import com.onstar.cn.mag.LocalVehicleEntity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;

import java.util.LinkedHashMap;

/**
 * Created by Rengui Lu on 2016/6/27.
 */
public class SmartRequestUtil {
    private static class RequestUtilInner {
        static SmartRequestUtil requestUtil = new SmartRequestUtil();
    }

    private SmartRequestUtil() {
    }

    public final static SmartRequestUtil getInstance() {
        return RequestUtilInner.requestUtil;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getSmartDevices(ResponseHandler responseHandler, Context context) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.getSmartDevices(ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin());
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getSmartDevices", ResponseHandler.class, Context.class);
    }

    private void addAuthorization(Context context, RequestEntity requestEntity) {
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getSmartDeviceDetail(ResponseHandler responseHandler, Context context, String deviceId, String deviceType) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.getSmartDeviceDetail(ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), deviceId,deviceType);
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getSmartDeviceDetail", ResponseHandler.class, Context.class, String.class,String.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void bindDevice(ResponseHandler responseHandler, Context context, String deviceId) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.bindDevice(ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), deviceId);
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "bindDevice", ResponseHandler.class, Context.class, String.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void unBindDevice(ResponseHandler responseHandler, Context context, String deviceId) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.unBindDevice(ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), deviceId);
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "unBindDevice", ResponseHandler.class, Context.class, String.class);
    }



    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void controlSmartDevice(ResponseHandler responseHandler, Context context, RequestEntity entity) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.controlSmartDevice(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin());
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "controlSmartDevice", ResponseHandler.class, Context.class, RequestEntity.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void saveSmartGeoFencing(ResponseHandler responseHandler, Context context, String deviceId, RequestEntity entity) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.saveSmartGeoFencing(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), deviceId);
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "saveSmartGeoFencing", ResponseHandler.class, Context.class, String.class, RequestEntity.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void batchSmartGeoFencing(ResponseHandler responseHandler, Context context, RequestEntity entity) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.batchApplySmartDeviceGeofencing(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin());
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "batchSmartGeoFencing", ResponseHandler.class, Context.class, RequestEntity.class);
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void localVehicleService(LocalVehicleEntity entity, ResponseHandler responseHandler, Context context) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.localVehicleService(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getCurrentVehicle().getVin());
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "localVehicleService", LocalVehicleEntity.class, ResponseHandler.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getServiceStatus(ResponseHandler responseHandler, Context context, RequestEntity entity, String smartHome) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.getServiceStatus(entity,ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(),smartHome);
        addAuthorization(context, requestEntity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getServiceStatus", ResponseHandler.class, Context.class,RequestEntity.class,String.class);
    }

}
